package com.lindroy.androidplayer.ui.knowledge

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseLazyFragment
import com.lindroy.androidplayer.base.eventViewModel
import com.lindroy.androidplayer.extensions.initColor
import com.lindroy.androidplayer.extensions.loadListData
import com.lindroy.androidplayer.extensions.setLineDivider
import com.lindroy.androidplayer.extensions.yes
import com.lindroy.androidplayer.logic.model.bean.CollectBus
import com.lindroy.androidplayer.ui.adapter.QuestionAdapter
import com.lindroy.androidplayer.ui.webview.ContentActivity
import com.lindroy.androidplayer.viewmodel.CollectViewModel
import com.lindroy.androidplayer.viewmodel.KnowledgeViewModel
import kotlinx.android.synthetic.main.fragment_knowledge_question.*
import kotlinx.android.synthetic.main.fragment_knowledge_question.refreshHeader
import kotlinx.android.synthetic.main.fragment_knowledge_question.stateLayout

/**
 * @author Lin
 * @date 2021/4/30
 * @function 问答
 * @Description
 */
class KnowledgeQuestionFragment(override val layoutId: Int = R.layout.fragment_knowledge_question) :
    BaseLazyFragment() {

    private val knowledgeViewModel: KnowledgeViewModel by viewModels()

    private val adapter by lazy { QuestionAdapter() }

    private val collectViewModel: CollectViewModel by viewModels()

    override fun initView() {
        refreshHeader.initColor()
        rvQuestion.setLineDivider()
        rvQuestion.adapter = adapter

    }

    override fun lazyLoad() {
        reqQuestions(true)
    }

    override fun initListener() {
        knowledgeViewModel.questionState.observe(this) {
            loadListData(it, adapter, refreshLayout, stateLayout)
        }
        //收藏的监听
        eventViewModel.collectEvent.observeInFragment(this) { collectBus ->
            val index = adapter.data.indexOfFirst {
                it.id == collectBus.originId
            }
            if (index >= 0) {
                adapter.data[index].collect = collectBus.collect
                adapter.notifyItemChanged(index)
            }
        }
        refreshLayout.setOnRefreshListener {
            reqQuestions(false)
        }
        refreshLayout.setOnLoadMoreListener {
            reqQuestions(false)
        }
        collectViewModel.collectState.observe(this) {
            if (it.success) {
                eventViewModel.collectEvent.value = CollectBus(it.id, it.collect, it.originId)
            }
        }
    }

    override fun initClick() {
        stateLayout.setOnViewsClickListener { state, view ->
            reqQuestions(true)
        }
        adapter.setOnCollectClickListener { collect, id ->
            collectViewModel.toggleCollectListArticle(collect, id)
        }
        adapter.setOnItemClickListener { _, view, position ->
            val item = adapter.data[position]
            ContentActivity.start(view.context, item.id, item.link, item.title, item.collect)
        }
    }

    private fun reqQuestions(showLoading: Boolean, isRefresh: Boolean = true) {
        showLoading.yes {
            stateLayout.showLoading()
        }
        knowledgeViewModel.reqQuestionList(isRefresh)
    }

    companion object {
        @JvmStatic
        fun newInstance() =
            KnowledgeQuestionFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }
}